package com.xmb.common.service.wx.impl;

import com.github.binarywang.wxpay.bean.entpay.EntPayRequest;
import com.github.binarywang.wxpay.bean.entpay.EntPayResult;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.EntPayService;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.EntPayServiceImpl;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.xmb.common.entity.XmbXcxAccountConfig;
import com.xmb.common.exception.BusinessException;
import com.xmb.common.interceptor.CurrentContextUser;
import com.xmb.common.params.WxEntPayParam;
import com.xmb.common.service.XcxAccountService;
import com.xmb.common.service.wx.WxEntPayService;
import com.xmb.common.utils.NumberUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 * Created by lyndon on 2019/3/26.
 * 微信企业付款
 */
@Service
public class WxEntPayServiceImpl implements WxEntPayService{

    @Resource
    private XcxAccountService accountService;

    public final static String PAYMENT_TYPE = "JSAPI";

    public final static String SPBILL_CREATE_IP = "127.0.0.1";

    private EntPayService entPayService;

    private WxPayService wxPayService;


    @Override
    public EntPayResult wxEntPay(WxEntPayParam entPayParam) throws WxPayException {
        // 设置支付配置信息
        XmbXcxAccountConfig xcxAccountConfig = accountService.selectByAppid(CurrentContextUser.get().getAppid());
        if (xcxAccountConfig == null) {
            throw new BusinessException("无法获取当前小程序的支付配置信息");
        }
        wxPayService = new WxPayServiceImpl();
        this.setWxEntPayConfig(xcxAccountConfig);
        entPayService = new EntPayServiceImpl(wxPayService);
        EntPayRequest entPayRequest = new EntPayRequest();
        entPayRequest.setPartnerTradeNo(this.getPartnerTradeNo(entPayParam.getUserId()));
        entPayRequest.setOpenid(entPayParam.getOpenid());
        entPayRequest.setCheckName(entPayParam.getCheckName());
        entPayRequest.setReUserName(entPayParam.getReUserName());
        entPayRequest.setAmount(NumberUtils.yuanToFee(entPayParam.getAmount().floatValue()));
        entPayRequest.setDescription(entPayParam.getDescription());
        entPayRequest.setSpbillCreateIp(SPBILL_CREATE_IP);
        return entPayService.entPay(entPayRequest);
    }


    /**
     * 设置支付配置信息
     */
    public void setWxEntPayConfig(XmbXcxAccountConfig xcxAccountConfig ) {
        wxPayService = new WxPayServiceImpl();
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(xcxAccountConfig.getMainAppid());
        payConfig.setMchId(xcxAccountConfig.getMainMchId());
        payConfig.setMchKey(xcxAccountConfig.getMainMchKey());
        payConfig.setTradeType(PAYMENT_TYPE);
        payConfig.setKeyPath(xcxAccountConfig.getKeyPath());
        if (xcxAccountConfig.getIsSubAccount()) {
            payConfig.setSubAppId(xcxAccountConfig.getSubAppid());
            payConfig.setSubMchId(xcxAccountConfig.getSubMchId());
        }
        wxPayService.setConfig(payConfig);
    }

    /**
     * 生成商户交易单号
     * @param userId
     * @return
     */
    private String getPartnerTradeNo(Long userId) {
        String str = "ENT_PAY"
                + "_"
                + LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"))
                + "_"
                + String.valueOf(userId).substring(0,4);
        return str.toUpperCase();
    }
}
